﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using Pap.Core.Utilities;
using Pap.Data;
using Pap.Data.Abstraction.Utils;
using Pap.Services;
using Pap.Services.WorkflowEngine;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Loan.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class ResponsibleWaitingListControlViewModel : IViewModel
    {
        /// <summary>
        /// 
        /// </summary>
        ILoanRepository _LoanRepository;
        /// <summary>
        /// 
        /// </summary>
        IWorkflowService _WfService;
        /// <summary>
        /// Initializes a new instance of the <see cref="ResponsibleWaitingListControlViewModel"/> class.
        /// </summary>
        /// <param name="loanRepo">The loan repo.</param>
        /// <param name="wf">The wf.</param>
        public ResponsibleWaitingListControlViewModel(ILoanRepository loanRepo, IWorkflowService wf)
        {
            this._LoanRepository = loanRepo;
            this._WfService = wf;
        }

        /// <summary>
        /// Gets the available decisions.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ActionTypes> GetAvailableDecisions()
        {
            return _WfService.GetAvailableActionsByStep(Steps.LoanCreated);
        }

        /// <summary>
        /// Gets the loans for current responsible.
        /// </summary>
        /// <param name="responsibleID">The responsible ID.</param>
        /// <returns></returns>
        public IEnumerable GetLoansForCurrentResponsible(Guid responsibleID)
        {
            var loansEntitiesIDs = _WfService.GetInStep(Steps.LoanCreated);

            var loans = _LoanRepository.FindCustomerLoans(loan => loansEntitiesIDs.Contains(loan.LoanID));

            var resultList = (from loan in loans
                              select new
                              {
                                  LoanID = loan.LoanID,
                                  LoanNumber = loan.LoanNumber,
                                  CustomerName = loan.CustomerObj.CustomerName,
                                  LoanCreatedDate = loan.LoanCreatedDate,
                                  LoanAmount = loan.LoanAmount,
                                  PaymentMethod = loan.LoanPaymentMethodObj.LoanPaymentMethodName,
                                  loanName = loan.LoanName,
                                  LoanStatus = _WfService.GetStep(_WfService.GetProcessInstanceIDByEntity(loan.LoanID)).DisplayName.ToString(),
                                  PreviousNotes = _WfService.GetStep((_WfService.GetProcessInstanceIDByEntity(loan.LoanID))).PreviousNotes,
                              }).OrderBy(loan => loan.LoanNumber);

            return resultList;
        }

        /// <summary>
        /// Takes the decision.
        /// </summary>
        /// <param name="loanID">The loan ID.</param>
        /// <param name="action">The action.</param>
        public void TakeDecision(Guid loanID, ActionTypes action)
        {
            using (IUnitOfWork transaction = ServiceLocator.Current.GetInstance<IUnitOfWork>())
            {
                Guid processInstanceID = _WfService.GetProcessInstanceIDByEntity(loanID);
                _WfService.Move(action, processInstanceID, RegistryInfo.UserID, "", null);

                transaction.Commit();
            }
        }

        /// <summary>
        /// Takes the loan decisions.
        /// </summary>
        /// <param name="loansIDs">The loans I ds.</param>
        /// <param name="notes">The notes.</param>
        /// <param name="loanDecisions">The loan decisions.</param>
        public void TakeLoanDecisions(List<Guid> loansIDs, List<string> notes, List<ActionTypes> loanDecisions)
        {
            using (IUnitOfWork trancaction = ServiceLocator.Current.GetInstance<IUnitOfWork>())
            {
                for (int i = 0; i < loanDecisions.Count; i++)
                {
                    Guid processInstanceID = _WfService.GetProcessInstanceIDByEntity(loansIDs[i]);
                    _WfService.Move(loanDecisions[i], processInstanceID, RegistryInfo.UserID, notes[i], null);
                }

                trancaction.Commit();
            }
        }
    }
}
